/**
 * 
 */
package com.nh.common;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.nh.entity.NgUser;
import com.nh.service.IBaseService;
import com.nh.utils.http.Information;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 项目名称：NotoginsengManage
 * 
 * 类名称：CoustemInterceptor
 * 
 * 类描述：
 * 
 * 创建人：李兴建
 * 
 * E-mail:lxj08193@163.com
 * 
 * 创建时间：2012-11-11 下午11:23:39
 * 
 * 修改人：李兴建
 * 
 * 修改时间：2012-11-11 下午11:23:39
 * 
 * 修改备注：
 * 
 * @version
 */
public class CustomerInterceptor extends AbstractInterceptor {
	private IBaseService baseService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com
	 * .opensymphony.xwork2.ActionInvocation)
	 */
	@Override
	public String intercept(ActionInvocation interceptor) throws Exception {
		HttpServletRequest request = (HttpServletRequest) interceptor.getInvocationContext().get(ServletActionContext.HTTP_REQUEST);
		String phoneNum = request.getParameter("phone");
		if (!validatePhoneNum(phoneNum)) {
			return Action.ERROR;// 验证不通过
		}
		return interceptor.invoke();
	}

	/**
	 * 验证手机号
	 * 
	 * @return
	 */
	private boolean validatePhoneNum(String phoneNum) {
		if (StringUtils.isNotEmpty(phoneNum)) {
			List list = this.getBaseService().findByProperty("phoneNum", phoneNum, NgUser.class);
			if (list.size() > 0) {
				return true;
			} else {
				// 第一次登录 调用webservice接口验证手机号是否是电信的，并存放数据库
				if (webserviceValidate(phoneNum)) {
					//获取访问者的IP
			        HttpServletRequest req=(HttpServletRequest)ServletActionContext.getRequest();
					String lastLoginIP = req.getHeader("x-forwarded-for");  
			         if(lastLoginIP == null || lastLoginIP.length() == 0 || "unknown".equalsIgnoreCase(lastLoginIP)) {  
			             lastLoginIP = req.getHeader("Proxy-Client-IP");  
			         }  
			         if(lastLoginIP == null || lastLoginIP.length() == 0 || "unknown".equalsIgnoreCase(lastLoginIP)) {  
			             lastLoginIP = req.getHeader("WL-Proxy-Client-IP");  
			         }  
			         if(lastLoginIP == null || lastLoginIP.length() == 0 || "unknown".equalsIgnoreCase(lastLoginIP)) {  
			             lastLoginIP = req.getRemoteAddr();  
			         }
			         
					NgUser user = new NgUser();
					user.setIp(lastLoginIP);
					user.setPhoneNum(phoneNum);
					user.setDiscription("电信用户");
					user.setPhonetype("android智能手机");
					user.setRegisterTime(new Timestamp(System.currentTimeMillis()));
					this.getBaseService().save(user);
					return true;
				} else {
					return false;
				}
			}

		}

		return false;
	}

	/**
	 * 调用webservice接口验证手机号是否是电信的
	 * 
	 * @param phoneNum
	 * @return
	 */
	private boolean webserviceValidate(String phoneNum) {
		if(true){
			return true;
		}
		String content = Information.getMobileNumerInfo(phoneNum);
		if (content.contains("您的号码不在中国电信移动业务服务范围内")) {
			return false;
		} else if (content.contains("您号码的归属地为")) {
			return true;
		}
		return true;
	}

	/**
	 * @return the baseService
	 */
	public IBaseService getBaseService() {
		return baseService;
	}

	/**
	 * @param baseService
	 *            the baseService to set
	 */
	@Resource
	public void setBaseService(IBaseService baseService) {
		this.baseService = baseService;
	}

}
